//package com.jd.jdoss.annotation;
//
//import com.alibaba.fastjson.JSON;
//import com.jd.jdoss.common.Result;
//import com.jd.jdoss.config.CusAccessObjectUtil;
//import com.jd.jdoss.excpetion.RequestLimitException;
//import lombok.extern.slf4j.Slf4j;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.OutputStreamWriter;
//import java.io.PrintWriter;
//import java.util.concurrent.TimeUnit;
//
//@Aspect
//@Component
//@Slf4j
//public class RequestLimitContract {
//
//    /**
//     * asfas
//     */
//    @Autowired
//    private RedisTemplate<String, String> redisTemplate;
//
//    @Before("within(@org.springframework.stereotype.Controller *) && @annotation(limit)")
//    public void requestLimit(final JoinPoint joinPoint, RequestLimit limit) throws RequestLimitException {
//        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
//
//        try {
//            Object[] args = joinPoint.getArgs();
//            HttpServletRequest request = null;
//            for (int i = 0; i < args.length; i++) {
//                if (args[i] instanceof HttpServletRequest) {
//                    request = (HttpServletRequest) args[i];
//                    break;
//                }
//            }
//            if (request == null) {
//                throw new RequestLimitException("方法中缺失HttpServletRequest参数");
//            }
//            String ip = CusAccessObjectUtil.getIpAddress(request);
//            String url = request.getRequestURL().toString();
//            String key = "req_limit_".concat(url).concat(ip);
//            long count = redisTemplate.opsForValue().increment(key, 1);
//            if (count == 1) {
//                redisTemplate.expire(key, limit.time(), TimeUnit.MILLISECONDS);
//            }
//            if (count > limit.count()) {
//                log.info("用户IP[" + ip + "]访问地址[" + url + "]超过了限定的次数[" + limit.count() + "]");
//                ret(response);
//            }
//        } catch (RequestLimitException e) {
//            throw e;
//        } catch (Exception e) {
//            log.error("发生异常: ", e);
//        }
//    }
//
//    public void ret(HttpServletResponse response) {
//        Result resultInfo =new Result(404,"请求数据快，请等待！","");
//        PrintWriter writer = null;
//        OutputStreamWriter osw = null;
//        try {
//            osw = new OutputStreamWriter(response.getOutputStream(),
//                    "UTF-8");
//            writer = new PrintWriter(osw, true);
//            String jsonStr = JSON.toJSONString(resultInfo);
//            writer.write(jsonStr);
//            writer.flush();
//            writer.close();
//            osw.close();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
//}